#include "privatechat.h"
#include "ui_privatechat.h"
#include <QTime>
#include <QDebug>
#include <QListWidgetItem>

PrivateChat::PrivateChat(QString sender, QString receiver, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PrivateChat), sender(sender), receiver(receiver)
{
    ui->setupUi(this);
    ui->label_name->setText(receiver);
    ui->te_chatFrame->setReadOnly(true);
    this->setWindowTitle(sender + "的私聊");
    this->setFixedSize(this->geometry().size()); // 禁止窗口尺寸改变
//    this->setWindowFlag(Qt::FramelessWindowHint); // 去掉标题栏
}


PrivateChat::~PrivateChat()
{
    delete ui;
}

void PrivateChat::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event)
    emit chatClose(receiver);
    this->hide();
}

// 根据消息来源设置文本块格式
void PrivateChat::setMessageFormat(const QString &message, Qt::Alignment alignment)
{
    int size = this->geometry().width() / 20;
//    //qDebug() << size;
    // 插入新消息
    for (auto str : splitByLength(message, size))
    {
        QTextCursor cursor = ui->te_chatFrame->textCursor();
        cursor.movePosition(QTextCursor::End);
        ui->te_chatFrame->setTextCursor(cursor);
        if (alignment == Qt::AlignRight && message.length() > size && str.length() < size)
        {
            ui->te_chatFrame->insertPlainText("  " + str + QString(size - str.length(), QChar(32)) + "  \n");
        }
        else
        {
            ui->te_chatFrame->insertPlainText("  " + str + "  \n");
        }

        cursor = ui->te_chatFrame->textCursor();
        cursor.movePosition(QTextCursor::PreviousBlock);
        QTextBlockFormat blockFormat = cursor.blockFormat();
        // 设置对齐方式
        blockFormat.setAlignment(alignment);
        cursor.setBlockFormat(blockFormat);
        ui->te_chatFrame->setTextCursor(cursor);
    }
}

void PrivateChat::setMessageTitleFormat(const QString &message, Qt::Alignment alignment)
{
    QTextCursor cursor = ui->te_chatFrame->textCursor();
    cursor.movePosition(QTextCursor::End);
    ui->te_chatFrame->setTextCursor(cursor);
    // 插入新消息
    ui->te_chatFrame->insertPlainText(message + "\n");
    cursor = ui->te_chatFrame->textCursor();
    cursor.movePosition(QTextCursor::PreviousBlock);
    QTextBlockFormat blockFormat = cursor.blockFormat();
    // 设置对齐方式
    blockFormat.setAlignment(alignment);
    cursor.setBlockFormat(blockFormat);
    ui->te_chatFrame->setTextCursor(cursor);
}

// 按指定长度分割QString
QStringList PrivateChat::splitByLength(const QString &input, int length)
{
    QStringList result;
        int startIndex = 0;
        while (startIndex < input.length())
        {
            QString segment = input.mid(startIndex, length);
            result.append(segment);
            startIndex += length;
        }
        return result;
}

// 发送消息槽函数
void PrivateChat::on_ptn_send_clicked()
{
    QString info = ui->te_info->toPlainText();
    if (info.isEmpty())
    {
        return ;
    }
    QString time = QTime::currentTime().toString();
    if (min != time.mid(3,2).toInt())
    {
        hour = time.mid(0,2).toInt();
        min = time.mid(3,2).toInt();
        setMessageTitleFormat(time, Qt::AlignCenter);
    }

    setMessageTitleFormat(this->sender + ":", Qt::AlignRight);

    setMessageFormat(info, Qt::AlignRight);
    ui->te_info->clear();
    emit sendInfo(sender, receiver, info, time);
}

// 接收消息槽函数
void PrivateChat::onNewInfo(messagePackage pack)
{
    QString time = pack.time();
    if (min != time.mid(3,2).toInt())
    {
        hour = time.mid(0,2).toInt();
        min = time.mid(3,2).toInt();
        setMessageTitleFormat(time, Qt::AlignCenter);
    }


    setMessageTitleFormat(this->receiver + ":", Qt::AlignLeft);

    setMessageFormat(pack.info(), Qt::AlignLeft);
}

